<?php
require_once('./modules/feeds/feedItem.inc');
require_once('./modules/cache.inc');
require_once('./modules/util.inc');

class FeedSourceRenderer extends Renderer
{
	protected $start;
	protected $ITEMS_IN_PAGE;
	protected $sourceId;
	
	protected $feedItems;
	
	function pageLoad()
	{
		$this->ITEMS_IN_PAGE = $this->config->get('./maxItemsPerPage');
		$this->start = $_GET['itemId'];
		$this->sourceId = $_GET['sourceId'];

		//loading it on page load so all functions can enjoy the info.
		$this->loadFeedItems();
		
		switch( $this->action )
		{
			case "":
			case "show":
			default:
				break;
		}
	}

	function loadFeedItems()
	{
		$oFeedItem = new FeedItem();
		
		//name the content type, with paging
		$cacheId = "feed-source-".$this->sourceId."-latest-".$this->start;

		$cacheStatus = 'DEAD';
		
		$cache = new Cache();
		
		if( !$cache->ERROR )
			$cacheStatus = $cache->check_cache($cacheId);
		
		if($cacheStatus == 'HIT')
			$this->feedItems = $cache->get($cacheId);
		else
		{
			$this->feedItems = array
			(
				'items' => $oFeedItem->getSourceItems($this->sourceId,'entry_date desc',$this->start,$this->ITEMS_IN_PAGE),
				'count' => $oFeedItem->getSourceCount($this->sourceId)
			);
			
			$cache->set($cacheId,$this->feedItems);
		}

	
	}
	
	function renderContent()
	{
		global $content;
		
		$paging = Util::getPaging($this->start,$this->feedItems['count'],"pg=feed_source&amp;sourceId=".$this->sourceId);
		$content .= $paging;
		
		if( count($this->feedItems['items']) > 0 )
			$content .= "<h1>Posts by ".$this->feedItems['items'][0]->feedName."</h1>";
		else
			$content .= "<p/><h2>No more items</h2><p/>";
			
		foreach($this->feedItems['items'] as $feedItem)
		{
			$feedText = Util::cleanTags($feedItem->content);
		
			$content .= 
			"<div>
				<h2><a href='".$feedItem->getUrl()."'>".$feedItem->title."</a></h2>
				<h5>".date('d/m/Y',$feedItem->entryDate)."</h5>
			</div>";

			$text = substr($feedText,0,min(strlen($feedText),1000));
			if( strlen($text) >3 && substr($text,strlen($text)-3,3) != '...')
				$text .= "...";

			$content .= "<div class='readableText'>".$text."</div><p/>";
			
		}
		$content .= $paging;
	}

	function setSEODetails()
	{
		global $title;
		global $description;
		
		$feed = new Feed();
		
		$feed->load($this->sourceId);
		
		$title = Util::cutStringFullWords($this->config->get('./siteName')." - ".$feed->feedName." page ".$this->start,65).".";
		$description = Util::cutStringFullWords($this->config->get('./siteName')." - ".$feed->feedName." - ".$feed->description." page ".$this->start,255).".";
	}
	
	
	function renderBreadcrumbs()
	{
		parent::renderBreadcrumbs();
		
		global $breadcrumbs;
		
		$feedName = $this->feedItems['items'][0]->feedName;
		
		$breadcrumbs .= "&nbsp;&#187;&nbsp;$feedName";
	}
	
}

$renderer = new FeedSourceRenderer();

?>